<?php
/**
 * 展位
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/28
 * Time: 23:05
 */
namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;
use think\Controller;
use PHPExcel_IOFactory;
class Booth extends Controller
{
    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        $this->model = model('Booth');
    }

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName="";
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     * 展位列表
     */
    public function getList(){
        $keyword = $this->request->param('keyword');
        $pavilion_id = $this->request->param('pavilion_id');
        $exhibition_id = $this->request->param('exhibition_id');
        $hall_id = $this->request->param('hall_id');
        $member_id = $this->request->param('member_id');
        $page = $this->request->param('page');

        $area_start = $this->request->param('area_start');
        $area_end = $this->request->param('area_end');

        $page = max(1,intval($page));
        $pagesize = empty($this->request->param('pageSize'))?10:$this->request->param('pageSize');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'展会信息错误']);
        }

        if(empty($hall_id)){
            return json(['code'=>0,'msg'=>'展厅信息错误']);
        }

        $param=" and m.is_delete=0 ";
        if(!empty($keyword)){
            $param.=" and (m.number like '%$keyword%') ";
        }





        // if(!empty($exhibition_id)){
        //     $where['exhibition_id'] = $exhibition_id;
        // }

        // if(!empty($member_id)){
        //     $where['member_id'] = $member_id;
        // }


        // if(!empty($area_start)&&!empty($area_end)){
        //     $where['area'] = array(array('EGT',$area_start),array('ELT',$area_end));
        // }elseif(!empty($area_start)){
        //     $where['area'] = array('EGT',$area_start);
        // }elseif(!empty($area_end)){
        //     $where['area'] = array('ELT',$area_end);
        // }

        $param.=" and m.hall_id=$hall_id and m.exhibition_id=$exhibition_id ";
        $tabName=$this->getTabName($exhibition_id);
        $sql2="select m.*,c.company_name as agent_info,c.status as company_status,pt.title as type_title,pt.price as type_price";
        $sql2.=" from crm_booth m ";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.agent_id";
        $sql2.=" left join crm_booth_mold as pt on pt.id=m.price_type";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by m.id desc ";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        $sql21=" select count(m.id) as count1 ";
        $sql21.=" from crm_booth as m where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        //var_dump($sql2);die();
        $list=Db::query($sql2);
        if(!empty($list)){
            foreach ($list as &$v){
                if($v['agent_id']>0){
                    $v['postion_status']=2;
                    if($v['company_status']>=5){
                        $v['postion_status']=4;
                    }
                }else{
                    $v['postion_status']=1;
                }
            }
            unset($v);
        }
        //$sum['area'] = $this->model->where($where)->limit(($page-1)*$pagesize.','.$pagesize)->sum('area');
        //$sum['price'] = $this->model->where($where)->limit(($page-1)*$pagesize.','.$pagesize)->sum('price');

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }

    /**
     * 展位详情
     * @return \think\response\Json|void
     */
    public function detail(){
        $id = $this->request->param('id');

        $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        if(empty($info)){
            return json(['code'=>0,'msg'=>'记录不存在']);
        }

        #展商信息
        $info['agentinfo'] = model('Companymgr')->findWhere(['id'=>$info['agent_id']]);
        $info['pavilion_title'] = db('pavilion')->where('id',$info['pavilion_id'])->value('title');
        $info['exhibition_title'] = db('exhibition')->where('id',$info['exhibition_id'])->value('title');
        $info['hall_title'] = db('hall')->where('id',$info['hall_id'])->value('title');

        #获取展商名称
        $public_id = model('company_follow')->where(['id'=>$info['agent_id']])->find();
        if($public_id){
            $info['zs_name'] = db('company_public_company')->where(['public_id'=>$public_id['public_id']])->value('zs_name');
            if(empty($info['zs_name'])){
                $info['zs_name'] = db('company')->where(['id'=>$public_id['company_id']])->value('company_name');
            }
        }

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     *
     * @return mixed
     */
    public function _before_add(){
        $data['exhibition_id'] = $this->request->param('exhibition_id');
        $data['pavilion_id'] = $this->request->param('pavilion_id');
        $data['hall_id'] = $this->request->param('hall_id');
        $data['area'] = $this->request->param('area');
        //$data['position'] = $this->request->param('position');
        //$data['describe'] = $this->request->param('describe');
        $data['price_type'] = $this->request->param('price_type');
        $data['height'] = $this->request->param('height');
        $data['width'] = $this->request->param('width');
        $data['price'] = $this->request->param('price');
        $data['number']  = $this->request->param('number');
        //$data['sort']  = $this->request->param('sort');
        $data['agent_id']  = $this->request->param('agent_id');
        $data['is_show']  = $this->request->param('is_show');
        $data['locking']  = $this->request->param('locking');
        $data['top']  = $this->request->param('top');
        $data['left']  = $this->request->param('left');
        $data['corner_stall']  = $this->request->param('corner_stall');
        return $data;
    }

    /**
     * 新增展位
     * @return \think\response\Json|void
     */
    public function add(){
        $data = $this->_before_add();

        $data['create_time']  = time();
        $data['is_delete']  = 0;
        //查看展位号码是否存在 by lhd 2020.2.10
        if(!empty($data['number'])){
            $zwh = $this->model->where(['number'=>$data['number'],'hall_id'=>$data['hall_id'],'is_delete'=>0])->find();
            if($zwh){
                return json(['code'=>0,'msg'=>'展位号不能重复']);
            }
        }


        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 编辑展位
     * @return \think\response\Json|void
     */
    public function edit(){
        $id = $this->request->param('id');
        $data = $this->_before_add();

        foreach ($data as $k=>$v){
            if(!is_numeric($v)&&empty($v)){
                unset($data[$k]);
            }
        }

        //查看展位号码是否存在 by lhd 2020.2.10
        $zwh = $this->model->where(['id'=>$id])->find();
        if(isset($data['number'])&&!empty($data['number'])&&$zwh['number']!=$data['number']){
            //改了展位号
            $zwh1 = $this->model->where(['number'=>$data['number'],'hall_id'=>$data['hall_id'],'is_delete'=>0])->find();
            if($zwh1) {
                return json(['code' => 0, 'msg' => '展位号不能重复']);
            }
        }

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }
        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除展位
     * @return \think\response\Json|void
     */
    public function del(){
        $ids = $this->request->param('id');
        $is_show = $this->request->param('is_show')?1:0;
        $id = explode(',',$ids);
        $is=true;$msg='';
        //首先进行数据验证
        foreach ($id as $val){
            $where = ['id'=>$val,'is_delete'=>0];
            $r = $this->model->where($where)->find();
            if(empty($r)){
                $msg='id:'.$val.'记录不存在，请核实!';
                $is=false;
                break;
            }

            if($r['agent_id']!='' || $r['agent_id']!=null || !empty($r['agent_id'])){
                $msg='id:'.$val.'此展位已绑定展商,请解除绑定再操作！';
                $is=false;
                break;
            }
        }
        if(!$is){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($id as $v){
            if($is_show == 1){
                $r = $this->model->updateBywhere(['is_show'=>1],['id'=>$v]);
            }else{
                $r = $this->model->deleteWhere(['id'=>$v]);
            }

            if(!$r){
                $is=false;
                $msg='id:'.$v.'操作失败';
            }
        }

        if(!$is){
            Db::rollback();
            return json(['code'=>0,'msg'=>$msg]);
        }
        Db::commit();
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    //展位面积列表
    public function areaIndex(){
        $type = $this->request->param('type');
        $res=db('both_type')->where(['type'=>$type,'is_delect'=>0])->select();
        return json(['code'=>1,'msg'=>'操作成功','data'=>$res]);
    }

    //展位面积删除
    public function areaDel(){
        $id = $this->request->param('id');
        $res=db('both_type')->where(['id'=>$id])->update(['is_delect'=>1]);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    //展位面积编辑
    public function areaEdit(){
        $data['type'] = $this->request->param('type');
        $id = $this->request->param('id');
        $data['number'] = $this->request->param('number');
        $data['title'] = $this->request->param('title');
        $res=db('both_type')->where(['id'=>$id])->update($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    //展位面积编辑
    public function areaAdd(){
        $data['type'] = $this->request->param('type');
        $data['number'] = $this->request->param('number');
        $data['title'] = $this->request->param('title');
        $res=db('both_type')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }


    //展位面积编辑
    public function getAllHall(){
        $exhibition_id = $this->request->param('exhibition_id');
        if(empty($exhibition_id)){
            return json(['code'=>1,'msg'=>'展会数据错误']);
        }
        $pavilionList=db('pavilion')->where(['exhibition_id'=>$exhibition_id])->select();
        if(is_array($pavilionList) && count($pavilionList>0)){
            $ids=[];
            foreach ($pavilionList as $v){
                $ids[]=$v['id'];
            }
            $uid_str=implode(',',$ids);
            $where['pavilion_id']=array('in', $uid_str);
            $where['is_delete']=0;
            $hallList=db('hall')->where($where)->select();
            if(!$hallList){
                return json(['code'=>0,'msg'=>'展厅数据错误']);
            }else{
                return json(['code'=>1,'msg'=>'操作成功','data'=>$hallList]);
            }
        }else{
            return json(['code'=>0,'msg'=>'展馆数据错误']);
        }
    }


    //保存展位规格
    public function setLongOrWidth(){
        $ids = $this->request->param('ids');
        $width=$this->request->param('width');
        $height=$this->request->param('height');
        $locking=$this->request->param('locking');
        $ecIds=explode(',',$ids);
        if(count($ecIds) == 0){
            return json(['code'=>0,'msg'=>'请选择一个展位']);
        }
        $data['width']=$width;
        $data['height']=$height;
        $data['area']=$width*$height;
        $data['top']=1;
        $data['left']=1;
        $data['locking']=$locking?$locking:0;
        foreach ( $ecIds as $v) {
            $where['id']=$v;
            $r = $this->model->updateBywhere($data,$where);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }

    public function newEdit(){
        $listArr = $this->request->param('listArr');
        $locking = $this->request->param('locking')?$this->request->param('locking'):0;
        $listArr=json_decode($listArr,true);
        foreach ( $listArr as $v) {
            $where['id']=$v['id'];
            $data['top']=$v['top'];
            $data['left']=$v['left'];
            $data['locking']=$locking;
            $r = $this->model->updateBywhere($data,$where);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    //查询所有展位信息
    public function getAllPostiionList(){
        $exhibition_id = $this->request->param('exhibition_id');
        $hall_id = $this->request->param('hall_id');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'展会信息错误']);
        }
        if(empty($hall_id)){
            return json(['code'=>0,'msg'=>'展厅信息错误']);
        }
        $param=" and m.is_delete=0 and m.hall_id=$hall_id and m.exhibition_id=$exhibition_id ";
        $tabName=$this->getTabName($exhibition_id);
        $sql2="select m.*,c.company_name as agent_info ";
        $sql2.=" from crm_booth m ";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.agent_id";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by m.id desc ";
        //var_dump($sql2);die();
        $list=Db::query($sql2);
        $sql21=" select sum(m.area) as sum ";
        $sql21.=" from crm_booth as m where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['sum']; //st
        if(!empty($list)){
            foreach ($list as &$v){
                $v['contract_id']=null;
                $v['contract_status']=null;
                $contract_list=db('contract')->where(['id_zs'=>$v['agent_id'],'id_zhui'=>$exhibition_id,'is_delete'=>0])->select();
                if(!empty($contract_list)){
                    foreach($contract_list as &$vv){
                        if($vv['id_zwei']){
                            $zwList=explode(',',$vv['id_zwei']);
                            if(in_array($v['id'],$zwList)){
                                $v['contract_id']=$vv['id'];
                                $v['contract_status']=$vv['s_status'];
                            }
                        }
                    }
                    unset($vv);
                }

                if($v['contract_id']){
                    $v['postion_status']=2;
                    if($v['contract_status'] == 1){
                        $v['postion_status']=3;
                        $ftotal=db('finance')->field('sum(total_fee) as f_total')->where(['contract_id'=>$v['contract_id']])->select();
                        if($ftotal[0]['f_total']>1){
                            $v['postion_status']=4;
                        }
                    }
                }else{
                    $v['postion_status']=1;
                }
            }
            unset($v);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>$list,'totalArea'=>$total]);
    }

    public function setPostionLocking(){
        $ids = $this->request->param('ids');
        $locking = $this->request->param('locking')?$this->request->param('locking'):0;
        $ids=explode(',', $ids);
        if(!is_array($ids) || count($ids) == 0){
            return json(['code'=>0,'msg'=>'数据错误']);
        }
        foreach ($ids as &$v){
            $res=db('booth')->where(['id'=>$v])->find();
            if($res && $res['locking']!=$locking){
                db('booth')->where(['id'=>$v])->update(['locking'=>$locking]);
            }
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }


    public function setAearLocking(){
        $ids = $this->request->param('ids');
        $locking = $this->request->param('locking')?$this->request->param('locking'):0;
        $ids=explode(',', $ids);
        if(!is_array($ids) || count($ids) == 0){
            return json(['code'=>0,'msg'=>'数据错误']);
        }
        foreach ($ids as &$v){
            $res=db('booth_area')->where(['id'=>$v])->find();
            if($res && $res['locking']!=$locking){
                db('booth_area')->where(['id'=>$v])->update(['locking'=>$locking]);
            }
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function HallareaDel(){
        $ids = $this->request->param('ids');
        $is_show = $this->request->param('is_show');
        $ids=explode(',', $ids);
        if(!is_array($ids) || count($ids) == 0){
            return json(['code'=>0,'msg'=>'数据错误']);
        }
        foreach ($ids as &$v){
            $res=db('booth_area')->where(['id'=>$v])->find();
            if($res){
                if($is_show == 1){
                    db('booth_area')->where(['id'=>$v])->update(['is_show'=>1]);
                }else{
                    db('booth_area')->where(['id'=>$v])->update(['is_delete'=>1]);
                }
            }
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function HallareaIndex(){
        $exhibition_id = $this->request->param('exhibition_id');
        $hall_id = $this->request->param('hall_id');
        if(empty($exhibition_id)){
            return json(['code'=>1,'msg'=>'展会信息错误']);
        }
        if(empty($hall_id)){
            return json(['code'=>1,'msg'=>'展厅信息错误']);
        }
        $where['exhibition_id']=$exhibition_id;
        $where['hall_id']=$hall_id;
        $where['is_delete']=0;
        $list=db('booth_area')->where($where)->select();
        return json(['code'=>1,'msg'=>'操作成功','data'=>$list]);
    }

    public function HallareaEdit(){
        $exhibition_id = $this->request->param('exhibition_id');
        $hall_id = $this->request->param('hall_id');
        $areaList = $this->request->param('areaList');
        $locking = $this->request->param('locking');
        if(empty($exhibition_id)){
            return json(['code'=>1,'msg'=>'展会信息错误']);
        }
        if(empty($hall_id)){
            return json(['code'=>1,'msg'=>'展厅信息错误']);
        }
        $areaList=json_decode($areaList,true);
        if(!is_array($areaList) || count($areaList) == 0){
            return json(['code'=>1,'msg'=>'区域信息错误']);
        }
        foreach ($areaList as &$v){
            $data['title']=$v['title'];
            $data['type']=$v['type'];
            $data['color_type']=$v['color_type'];
            $data['opcity']=$v['opcity'];
            $data['width']=$v['width'];
            $data['height']=$v['height'];
            $data['top']=$v['top'];
            $data['left']=$v['left'];
            $data['color']=$v['color'];
            $data['locking']=$locking?$locking:0;
            $data['update_time']=time();
            $data['exhibition_id']=$exhibition_id;
            $data['hall_id']=$hall_id;
            $data['transform']=$v['transform'];
            $data['is_show']=$v['is_show'];
            if(!empty($v['id'])){
                $data['create_time']=time();
                $result=db('booth_area')->where(['id'=>$v['id']])->update($data);
            }else{
                $result=db('booth_area')->insert($data);
            }

        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }

    public function HallareaAloneEdit(){
        $id = $this->request->param('id');
        $width = $this->request->param('width');
        $height = $this->request->param('height');
        $top = $this->request->param('top');
        $left = $this->request->param('left');
        $data=array();
        if(!empty($width)){
            $data['width']=$width;
        }
        if(!empty($height)){
            $data['height']=$height;
        }
        if(!empty($top)){
            $data['top']=$top;
        }
        if(!empty($left)){
            $data['left']=$left;
        }
        //var_dump($data);die();
        $result=db('booth_area')->where(['id'=>$id])->update($data);
        if(!$result){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function postAloneEdit(){
        $id = $this->request->param('id');
        $top = $this->request->param('top');
        $left = $this->request->param('left');
        $data=array();
        if(!empty($top)){
            $data['top']=$top;
        }
        if(!empty($left)){
            $data['left']=$left;
        }
        $result=db('booth')->where(['id'=>$id])->update($data);
        if(!$result){
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        return json(['code'=>1,'msg'=>'操作失败']);
    }


    // 展位类别接口
    //展位类别列表
    public function getListType()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $list=db('booth_mold')->where(['is_delete'=>0,'exhibition_id'=>$exhibition_id])->select();
            return json(array('code' => '1', 'msg' => '操作成功','data'=>$list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //展位类别新增
    public function addType()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $title = isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $price = isset($post['price']) && $post['price'] != '' ? $post['price'] : 0;
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            if(!$title){
                return json(array('code' => '0', 'msg' => '名称未填写'));
            }
            $data['update_time']=time();
            $data['create_time']=time();
            $data['title']=$title;
            $data['price']=$price;
            $data['exhibition_id']=$exhibition_id;
            $res=db('booth_mold')->insert($data);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作失败'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //展位类别编辑
    public function editType()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $title = isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $price = isset($post['price']) && $post['price'] != '' ? $post['price'] : 0;
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $rPostion=db('booth_mold')->where(['id'=>$id])->find();
            if(!$rPostion){
                return json(array('code' => '0', 'msg' => '展位类别信息错误'));
            }
            if(!$title){
                return json(array('code' => '0', 'msg' => '名称未填写'));
            }
            $data['update_time']=time();
            $data['title']=$title;
            $data['price']=$price;
            $data['exhibition_id']=$exhibition_id;
            $res=db('booth_mold')->where(['id'=>$id])->update($data);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作失败'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //展位类别删除
    public function delType()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $rPostion=db('booth_mold')->where(['id'=>$id])->find();
            if(!$rPostion){
                return json(array('code' => '0', 'msg' => '展位类别信息错误'));
            }
            $res=db('booth_mold')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //查询展会所在的区域
    public function boothAreaList(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $list=db('booth')->Distinct(true)->field('area_code')->where(['exhibition_id'=>$exhibition_id,'area_code'=>['<>','not null'],'is_delete'=>0])->order('area_code asc')->select();
            foreach ($list as &$vv) {
                $vv['total'] = db('booth')->where(['area_code' => $vv['area_code'],'exhibition_id'=>$exhibition_id,'is_delete'=>0])->count();
            }
            $total=db('booth')->where(['exhibition_id'=>$exhibition_id,'is_delete'=>0])->count();
            return json(array('code' => '1', 'msg' => '操作成功','data'=>$list,'total'=>$total));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //查询具体区域数据
    public function boothSaleList(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $area_code = isset($post['area_code']) && $post['area_code'] != '' ? $post['area_code'] : '';
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $param=" and m.is_delete=0 and m.exhibition_id=$exhibition_id ";
            if($area_code){
                $param.=" and m.area_code='$area_code'";
            }
            // 1已付款 2，未付款 3，可出售
            $tabName=$this->getTabName($exhibition_id);
            $sql2="select m.*,c.company_name as agent_info,c.status as company_status";
            $sql2.=" from crm_booth m ";
            $sql2.=" left join crm_".$tabName." as c on c.id=m.agent_id";
            $sql2.=" where 1=1 $param";
            $sql2.=" order by m.id desc ";
//            var_dump($sql2);die();
            $list=Db::query($sql2);
            $infos=array(
                array('status_name'=>'已付款','status'=>1,'booth_child'=>array()),
                array('status_name'=>'未付款','status'=>2,'booth_child'=>array()),
                array('status_name'=>'可出售','status'=>3,'booth_child'=>array())
            );
            $child1=array();$child2=array();$child3=array();
            if(!empty($list)){
                foreach ($list as &$v){
                    if(!$v['agent_id'] || $v['agent_id']==null || empty($v['agent_id'])){
                        $child3[]=$v;
                    }else{
                        if($v['company_status']>4){
                            $child1[]=$v;
                        }else{
                            $child2[]=$v;
                        }
                    }
                }
                unset($v);
                $infos[0]['booth_child']=$child1;
                $infos[1]['booth_child']=$child2;
                $infos[2]['booth_child']=$child3;
            }
            return json(['code'=>1,'msg'=>'操作成功','data'=>$infos]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }




    //excel上传
    public function excelupload(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $hall_id = isset($post['hall_id']) && $post['hall_id'] != '' ? $post['hall_id'] : 0;
            $pavilion_id = isset($post['pavilion_id']) && $post['pavilion_id'] != '' ? $post['pavilion_id'] : 0;
            $file = isset($post['file']) && $post['file'] != '' ? $post['file'] : null;
            if(!$file){
                return json(array('code' => '0', 'msg' => 'EXCEL信息错误'));
            }
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $resHall=db('hall')->where(['is_delete'=>0,'id'=>$hall_id])->find();
            if(!$resHall){
                return json(array('code' => '0', 'msg' => '展厅数据错误'));
            }

            $resPav=db('pavilion')->where(['is_delete'=>0,'id'=>$pavilion_id])->find();
            if(!$resPav){
                return json(array('code' => '0', 'msg' => '展馆数据错误'));
            }

            $filename = ROOT_PATH.'public/uploads/excel/'.$file;
            $reader = PHPExcel_IOFactory::createReaderForFile($filename);
            $excel = $reader->load($filename, $encode = 'utf-8');
            $excel_array=$excel->getsheet(0)->toArray();  //转换为数组格式
            $list=array();
            array_shift($excel_array);  //删除第一个数组(标题);
            if (!is_array($excel_array) || empty($excel_array) || count($excel_array)==0) {
                return json(['code' => 0, 'msg' => '获取数据失败！']);
            } else {
                foreach ($excel_array as $key => $value) {
                    $list[$key]['number'] = (string) $value[0];//展位编号
                    $list[$key]['width'] = (string) $value[1]; //宽
                    $list[$key]['height'] = (string) $value[2]; //高
                    $list[$key]['area'] = (string) $value[3]; // 面积
                    $list[$key]['price_type'] = (string) $value[4]; // 价格类别
                    $list[$key]['price'] = (string) $value[5]; // 价格类别
                    $list[$key]['area_code'] = (string) $value[6]; // 区域
                    $list[$key]['corner_stall'] = (string) $value[7]; // 角摊费用
                    $list[$key]['hall_id'] = $hall_id; // 展厅
                    $list[$key]['exhibition_id'] = $exhibition_id; // 展会
                    $list[$key]['pavilion_id'] = $pavilion_id; // 展会
                }
                $result=db('booth')->insertAll($list);
                if(!$result){
                    return json(['code'=>0,'msg'=>'操作失败']);
                }
                return json(['code'=>1,'msg'=>'操作成功']);
            }

        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


}